package com.example.controller;

import com.example.common.Result;
import com.example.entity.Series;
import com.example.service.SeriesService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/series")
public class SeriesController {

    @Resource
    private SeriesService seriesService;

    @GetMapping("/selectPage")
    public Result selectPage(Series series, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "4") Integer pageSize){
        PageInfo<Series> pageInfo = seriesService.selectPage(series,pageNum,pageSize);
        return Result.success(pageInfo);
    }

    //获取用户推荐
    @GetMapping("/selectByUser/{id}")
    public Result selectByUser(@PathVariable Integer id){
        List<Series> list = seriesService.selectByUser(id);
        return Result.success(list);
    }

    //获取用户评论
    @GetMapping("/selectByComment/{id}")
    public Result selectByComment(@PathVariable Integer id){
        List<Series> list = seriesService.selectByComment(id);
        return Result.success(list);
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id){
        Series series = seriesService.selectById(id);
        return Result.success(series);
    }

    @GetMapping("/selectRecommend/{id}")
    public Result selectRecommend(@PathVariable Integer id){
        List<Series> list = seriesService.selectRecommend(id);
        return Result.success(list);
    }

    @PostMapping("/add")
    public Result add(@RequestBody Series series){
        seriesService.add(series);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Series series){
        seriesService.update(series);
        return Result.success();
    }

    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Integer id){
        seriesService.delete(id);
        return Result.success();
    }

    @DeleteMapping("/delete-batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        seriesService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("/getSeriesRecommend")
    public Result getSeriesRecommend() {
        try {
            return Result.success(seriesService.getSeriesRecommend());
        }catch (Exception e){
            System.out.println("获取推荐列表失败");
            return Result.error();
        }
    }
}
